/**
 * @file pt_log.h
 * @brief log调试宏头文件
 * @author yejiahao
 * @date 2023-07-24 11:10:22
 * 
 * @copyright Copyright (c) 2023 by 深圳市鹏天微智能科技有限公司, All Rights Reserved.
 */

#ifndef PT_LOG_H
#define PT_LOG_H

#define LOG_ERROR   (0)
#define LOG_WARN    (1)
#define LOG_INFO    (2)
#define LOG_DEBUG   (3)

/*********************
 *      config
 *********************/

/* 根据不同平台选择打印函数 */
#include <stdio.h>
#define _printf             printf
#define ENABLE_LOG_COLOR
// #define ENABLE_LOG_TAG
#define LOG_LEVEL           (LOG_DEBUG)

/*********************
 *      log
 *********************/

#ifndef _printf
    #warning '_printf' macro undefined! use 'printf'
    #include <stdio.h>
    #define _printf  printf
#endif

#ifdef ENABLE_LOG_TAG
/* 为了防止redefine, 每个模块.c文件里定义tag前需要先undef一下 */
#undef PT_LOG_TAG
#define PT_LOG_TAG  "PT_LOG"
#endif

/* 使能彩色log输出 */
#ifdef ENABLE_LOG_COLOR
#define COLOR_NONE         "\033[m"
#define COLOR_RED          "\033[0;32;31m"
#define COLOR_LIGHT_RED    "\033[1;31m"
#define COLOR_GREEN        "\033[0;32;32m"
#define COLOR_LIGHT_GREEN  "\033[1;32m"
#define COLOR_BLUE         "\033[0;32;34m"
#define COLOR_LIGHT_BLUE   "\033[1;34m"
#define COLOR_DARY_GRAY    "\033[1;30m"
#define COLOR_CYAN         "\033[0;36m"
#define COLOR_LIGHT_CYAN   "\033[1;36m"
#define COLOR_PURPLE       "\033[0;35m"
#define COLOR_LIGHT_PURPLE "\033[1;35m"
#define COLOR_BROWN        "\033[0;33m"
#define COLOR_YELLOW       "\033[1;33m"
#define COLOR_LIGHT_GRAY   "\033[0;37m"
#define COLOR_WHITE        "\033[1;37m"

/* 配置log颜色 */
#define LOG_COLOR_WARN      COLOR_BROWN     // 棕色
#define LOG_COLOR_DEBUG     COLOR_NONE      // 无色（白）
#define LOG_COLOR_INFO      COLOR_GREEN     // 绿色
#define LOG_COLOR_ERROR     COLOR_RED       // 红色

#endif


#ifdef ENABLE_LOG_TAG
    #ifdef ENABLE_LOG_COLOR
        #define pt_printf(lvl, color, fmt, ...)      _printf(color "%c/%s/%03d> " COLOR_NONE fmt, lvl, PT_LOG_TAG, __LINE__, ##__VA_ARGS__)
    #else
        #define pt_printf(lvl, fmt, ...)      _printf("%c/%s/%03d> " fmt, lvl, PT_LOG_TAG, __LINE__, ##__VA_ARGS__)
    #endif
#else
    #ifdef ENABLE_LOG_COLOR
        #define pt_printf(lvl, color, fmt, ...)      _printf(color "%c/%03d> " COLOR_NONE fmt, lvl, __LINE__, ##__VA_ARGS__)
    #else
        #define pt_printf(lvl, fmt, ...)      _printf("%c/%03d> " fmt , lvl, __LINE__, ##__VA_ARGS__)
    #endif
#endif

#ifndef PT_LOG
#define PT_LOG(...)         _printf(__VA_ARGS__)
#endif

#if (LOG_LEVEL >= LOG_DEBUG)
#ifdef ENABLE_LOG_COLOR
#define PT_LOGD(...)        pt_printf('D', LOG_COLOR_DEBUG, __VA_ARGS__)
#else
#define PT_LOGD(...)        pt_printf('D', __VA_ARGS__)
#endif
#else
#define PT_LOGD(...)        ((void)0)
#endif

#if (LOG_LEVEL >= LOG_INFO)
#ifdef ENABLE_LOG_COLOR
#define PT_LOGI(...)        pt_printf('I', LOG_COLOR_INFO, __VA_ARGS__)
#else
#define PT_LOGI(...)        pt_printf('I', __VA_ARGS__)
#endif
#else
#define PT_LOGI(...)        ((void)0)
#endif

#if (LOG_LEVEL >= LOG_WARN)
#ifdef ENABLE_LOG_COLOR
#define PT_LOGW(...)        pt_printf('W', LOG_COLOR_WARN, __VA_ARGS__)
#else
#define PT_LOGW(...)        pt_printf('W', __VA_ARGS__)
#endif
#else
#define PT_LOGW(...)        ((void)0)
#endif

#if (LOG_LEVEL >= LOG_ERROR)
#ifdef ENABLE_LOG_COLOR
#define PT_LOGE(...)        pt_printf('E', LOG_COLOR_ERROR, __VA_ARGS__)
#else
#define PT_LOGE(...)        pt_printf('E', __VA_ARGS__)
#endif
#else
#define PT_LOGE(...)        ((void)0)
#endif


/**
 * @brief 以十进制的方式打印 buffer
 */
#define pt_log_buffer(pbuffer, len)    do { \
        for (int i = 0; i < len; i++) \
        { \
            _printf("%d ", *((pbuffer) + i)); \
        } \
        _printf("\n"); \
    } while (0)

/**
 * @brief 以十六进制的方式打印 buffer
 */
#define pt_log_buffer_hex(pbuffer, len)    do { \
        for (int i = 0; i < len; i++) \
        { \
            _printf("%02X ", *((pbuffer) + i)); \
        } \
        _printf("\n"); \
    } while (0)

/**
 * @brief 按位打印
 */
#define pt_log_bit(data, bit_len)    do { \
    for (int i = 0; i < bit_len; i++) \
    { \
        if ((i > 0) && ((i % 4) == 0)) \
        { \
            _printf(" "); \
        } \
        _printf("%d", (data & (1 << (bit_len - 1 - i))) ? 1 : 0); \
    } \
    _printf("\n"); \
    } while (0)

#endif /* PT_LOG_H */
